/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.editura.oxygen.severin.imageviewer.features;

import de.editura.oxygen.severin.imageviewer.features.knownDatamodels.ImageRefField;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

/**
 *
 * @author kschmidtner
 */
public class ImageReferenceSearch {

    //Tokens for Elements
    private final String ELEMENT_START = "<";
    private final String ELEMENT_END = ">";
    private final String ELEMENT_CLOSE_START = "</";
    
    //Tokens for Attributes
    private final String ATTRIBUTE_START = "=\"";
    private final String ATTRIBUTE_END = "\"";


    /**
     * This simple String comparison should be replaced with a simple parser.
     * 
     * @param doc
     * @param offset
     * @param imgRefField
     * @return 
     */
    public String searchForImgValue (Document doc, int offset, ImageRefField imgRefField) {
            
            String openTagToSearchFor = "";
            String endTagToSearchFor = "";

            switch (imgRefField.getXmlType()) {
                case ELEMENT:
                    openTagToSearchFor = ELEMENT_START + imgRefField.getName() + ELEMENT_END;
                    endTagToSearchFor = ELEMENT_CLOSE_START + imgRefField.getName() + ELEMENT_END;
                    break;
                case ATTRIBUTE:
                    openTagToSearchFor = imgRefField.getName() + ATTRIBUTE_START;
                    endTagToSearchFor = ATTRIBUTE_END;
                    break;
                    
            }
            
            int offsetStartOpeningTag = searchForwardsPosition(doc, offset, openTagToSearchFor);
            int offsetStartClosingTag = searchForwardsPosition(doc, offsetStartOpeningTag + openTagToSearchFor.length() - 1, endTagToSearchFor);
            String imgName = null;
            try {
                imgName = doc.getText((offsetStartOpeningTag + openTagToSearchFor.length()) - 1,
                        (offsetStartClosingTag - offsetStartOpeningTag) - openTagToSearchFor.length());
                //Todo Debuginfo: System.out.println("End of Start-Tag: " + offsetStartStartTag);
                //Todo Debuginfo: System.out.println("Start of End-Tags: " + offsetStartEndTag);
                //Todo Debuginfo: System.out.println("imgName: " + imgName);
                imgName = imgName.replace("\\", "/");
            } catch (BadLocationException ex) {
                //Todo Debuginfo: System.out.println("BadLocationException");
                Logger.getLogger(TextEditorCaretListener.class.getName()).log(Level.SEVERE, null, ex);
            }
            return imgName;
    }


    public int searchBackwardsPosition (Document doc, int offset, String searchForOpenTag) {
        int startImgOffset = 0;
        while (offset > 0) {
                try {
                    String compString = doc.getText(offset, searchForOpenTag.length());
                    offset--;
                    if (compString.equals(searchForOpenTag)) {
                        return offset + searchForOpenTag.length();
                    }
                } catch (BadLocationException ex) {
                    Logger.getLogger(TextEditorCaretListener.class.getName()).log(Level.SEVERE, null, ex);
                    offset = 0;
                }

            }
        return startImgOffset;
    }

    /**
     * Der Rückgabewert ist der Anfang des Tags
     * @param doc
     * @param offset
     * @param searchForEndTag
     * @return
     */
    public int searchForwardsPosition (Document doc, int offset, String searchForEndTag) {
        int endImgOffset = 0;
        while (offset <  doc.getLength() - searchForEndTag.length()) {
                try {
                    //offset--;
                    String compString = doc.getText(offset, searchForEndTag.length());
                    offset++;
                    //System.out.println("CompString - EndTag: " + compString);
                    if (compString.equals(searchForEndTag)) {
                        //System.out.println("Found End Tag at " + offset);
                        return offset;
                    }
                } catch (BadLocationException ex) {
                    System.out.println("Bad Location Exception");
                    Logger.getLogger(TextEditorCaretListener.class.getName()).log(Level.SEVERE, null, ex);
                    offset = doc.getLength() - searchForEndTag.length();
                }

            }
        return endImgOffset;
    }

    public String getEndTag() {
        return ELEMENT_END;
    }

    public String getStartEndTag() {
        return ELEMENT_CLOSE_START;
    }

    public String getStartOpenTag() {
        return ELEMENT_START;
    }



}
